<?php

namespace app\data\controller\api;

use app\data\model\ShopGoods;
use app\data\model\ShopGoodsCate;
use app\data\model\ShopGoodsMark;
use app\data\service\ExpressService;
use app\data\service\GoodsService;
use think\admin\Controller;

/**
 * 商品数据接口
 * Class Goods
 * @package app\data\controller\api
 */
class Goods extends Controller
{
    /**
     * 获取分类数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCate()
    {
        $this->success('获取分类成功', ShopGoodsCate::treeData());
    }
    
    //获取首页分类
    public function getIndexCate()
    {
        $this->success('获取分类成功', ShopGoodsCate::order('sort','desc')->limit(9)->select());
    }
    
    //获得分类商品数据
    public function getCateShop()
    {
        $index = $this->request->param('clickIndex',0);
        $priceOrder = $this->request->param('priceOrder',0);
        $pagenum = $this->request->param('pagenum','');
        $cateid = $this->request->param('cateids','');
        $type = $this->request->param('type','');
        // $data = [];
        $shoop = new ShopGoods();
        if($type === 'fid'){
            //获得第一级Fid分类商品数据
            $select = $shoop->CateFidShop($cateid,$pagenum,$index,$priceOrder);
        }else {
            //获得第三级tid分类商品数据
             $select = $shoop->CateShop($cateid,$pagenum,$index,$priceOrder);
        }
        
        $this->success('获取tid商品成功',$select);
    }
    
    //搜索数据
    public function getSearch(){
        $index = $this->request->param('clickIndex',0);
        $priceOrder = $this->request->param('priceOrder',0);
        $pagenum = $this->request->param('pagenum','');
        $searchVal = $this->request->param('searchVal','');
        $shop = new ShopGoods();
        $select = $shop->SearchData($searchVal,$pagenum,$index,$priceOrder);
        $this->success('获取搜索商品成功',$select);
    }

    /**
     * 获取标签数据
     */
    public function getMark()
    {
        $this->success('获取标签成功', ShopGoodsMark::items());
    }

    /**
     * 获取商品数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoods()
    {
        // 更新访问统计
        $map = $this->_vali(['code.default' => '']);
        if ($map['code']) ShopGoods::mk()->where($map)->inc('num_read')->update([]);
        // 商品数据处理
        $query = ShopGoods::mQuery()->like('name,marks,cateids,payment')->equal('code,vip_entry');
        $result = $query->where(['deleted' => 0, 'status' => 1])->order('sort desc,id desc')->page(true, false, false ,20);
        if (count($result['list']) > 0) GoodsService::instance()->bindData($result['list']);
        $this->success('获取商品数据', $result);
    }
    
    //获取全部商品列表
    public function getAllGoods()
    {
        $pagenum = $this->request->param('pagenum','');
        // $query = ShopGoods::mQuery()->like('name,marks,cateids,payment')->equal('code,vip_entry');
        $result = ShopGoods::where(['deleted' => 0, 'status' => 1])->order('sort desc,id desc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
        foreach ($result as $items){
            $data[]=$this->list_tree($items); //调用下面的方法
        }
        // if (count($result['data']) > 0) GoodsService::instance()->bindData($result['data']);
        $this->success('获取商品数据', $result);
    }
    
    // public function getAllGoods(){
    //     $data = [];
    //     $pagenum = $this->request->param('pagenum','');
    //     $select = ShopGoods::order('id','desc')->paginate(['list_rows'=> 10,'page' => $pagenum]);
    //     foreach ($select as $items){
    //         $data[]=$this->list_tree($items); //调用下面的方法
    //     }
    //     $this->success('获取商品成功',$select);
    // }
    
    // 获取单条商品数据
    public function getDetail(){
        $id = $this->request->param('id',0);
        if($id == 0){
            $this->error('商品id为空');
        }
        $detail = ShopGoods::where('id',$id)->find();
        if($detail == null){
            $this->error('无法获取商品详情');
        }
        $data=$this->list_tree($detail); //调用下面的方法
        $this->success($data);
    }
    // getDetail字符串转数组
    public function list_tree($data){
        $data['data_items'] = json_decode($data['data_items']);
        $data['data_specs'] = json_decode($data['data_specs']);
        $data['slider'] = explode('|', $data['slider']);
        $data['cateids'] = explode(',', $data['cateids']);
        return $data;
    }
    

    /**
     *  获取配送区域
     */
    public function getRegion()
    {
        $this->success('获取区域成功', ExpressService::instance()->region(3, 1));
    }
}